#include "stdafx.h"
#include "wf_zoomwindow.h"

#include "lx_define.h"
#include "pf_snapper.h"
#include "pf_point.h"


/**
 * Default constructor.
 *
 * @param keepAspectRatio Keep the aspect ratio. true: the factors 
 *          in x and y will stay the same. false Exactly the chosen 
 *          area will be fit to the viewport.
 */
WF_ZoomWindow::WF_ZoomWindow(PF_Container& container,
        PF_GraphicView& graphicView, bool keepAspectRatio)
        : PF_PreviewActionInterface("Zoom Window",
                            container, graphicView) {

    this->keepAspectRatio = keepAspectRatio;
}


QAction* WF_ZoomWindow::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Window Zoom")
	QAction* action = new QAction(tr("&Window Zoom"), NULL);
#if QT_VERSION >= 0x040600
        action->setIcon(QIcon::fromTheme("zoom-select", QIcon(":/actions/zoomwindow.png")));
#else
        action->setIcon(QIcon(":/actions/zoomwindow.png"));
#endif
        //action->zetStatusTip(tr("Zooms in a window"));
	
    return action;
}


void WF_ZoomWindow::init(int status) {
    PF_DEBUG->print("PF_ActionZoomWindow::init()");

    PF_PreviewActionInterface::init(status);
    v1 = v2 = PF_Vector(false);
    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_ZoomWindow::trigger() {
    PF_DEBUG->print("PF_ActionZoomWindow::trigger()");

    PF_PreviewActionInterface::trigger();

    if (v1.valid && v2.valid) {
        deletePreview();
        if (graphicView->toGuiDX(v1.distanceTo(v2))>5) {
            graphicView->zoomWindow(v1, v2, keepAspectRatio);
            init();
        }
    }
}



void WF_ZoomWindow::mouseMoveEvent(PF_MouseEvent* e) {
    if (getStatus()==1 && v1.valid) {
        v2 = snapPoint(e);
        deletePreview();
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(PF_Vector(v1.x, v1.y),
                                                   PF_Vector(v2.x, v1.y))));
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(PF_Vector(v2.x, v1.y),
                                                   PF_Vector(v2.x, v2.y))));
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(PF_Vector(v2.x, v2.y),
                                                   PF_Vector(v1.x, v2.y))));
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(PF_Vector(v1.x, v2.y),
                                                   PF_Vector(v1.x, v1.y))));
        drawPreview();
    }
}



void WF_ZoomWindow::mousePressEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case 0:
            v1 = snapPoint(e);
            setStatus(1);
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionZoomWindow::mousePressEvent(): %f %f",
                    v1.x, v1.y);
}



void WF_ZoomWindow::mouseReleaseEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionZoomWindow::mouseReleaseEvent()");

    if (e->button()==Qt::RightButton) {
        if (getStatus()==1) {
            deletePreview();
        }
        init(getStatus()-1);
    } else if (e->button()==Qt::LeftButton) {
        if (getStatus()==1) {
            v2 = snapPoint(e);
            trigger();
        }
    }
}



void WF_ZoomWindow::updateMouseButtonHints() {
    PF_DEBUG->print("PF_ActionZoomWindow::updateMouseButtonHints()");

    switch (getStatus()) {
    case 0:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first edge"), tr("Cancel"));
        break;
    case 1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second edge"), tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ZoomWindow::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::MagnifierCursor);
}


// EOF
